import threading

import talib as tl
import datetime
from SimulationTrade import User
import SimulationTrade as ST

ST.config("2021-8-09", "2021-8-18")
ST.openEntrustInfo = True
ST.openAutoEntrust = False  # 关闭自动委托
user = User(money=15000)
# stock0 = ST.Stock("603232.SH")
# stock1 = ST.Stock("603261.SH")
stopLossPrice = 0
createPrice = 0
isCross = 0
buyDays = 0
sellDays = 0
nowDays = 1

futures = ST.Futures("SC2112.INE")


def role1(stock):
    global buyDays, sellDays, isCross, nowDays
    avg = tl.MA(stock.getOldDayData.close_price.values[-50:].astype(float), 5)
    nowDays += 1
    isCross = 0
    if float(stock.getOldDayData.close_price.values[-2]) > float(avg[-2]) and \
            float(stock.getOldDayData.close_price.values[-1]) > float(avg[-1]):  # 突破
        print("↑↑↑↑突破↑↑↑↑")
        isCross = 1

    if float(stock.getOldDayData.close_price.values[-2]) > float(avg[-2]) and \
            float(stock.getOldDayData.close_price.values[-1]) < float(avg[-1]):  # 跌破
        print("↓↓↓↓跌破↓↓↓↓")
        isCross = -1

    if float(stock.getOldDayData.close_price.values[-1]) < float(avg[-1]):
        isUpAvg = False
        print("<<<<低于日均<<<<")
    else:
        isUpAvg = True
        print(">>>>高于日均>>>>")

    if buyDays == 0 and isCross == -1 and user.money >= stock.nowPrice:  # 购买日期小于天数  并且  有跌破
        buyDays = nowDays + 5  # 设置下次购买日期
        print("预计:", buyDays, "日购买")
    if buyDays >= nowDays and isUpAvg == True:  # 购买日期未到  并且  高于日均
        print("【取消购买】")
        buyDays = 0  # 重置购买日期
    if buyDays == nowDays and nowDays != 0:  # 收购  重置购买日期
        buyDays = 0
        num = user.money // stock.nowPrice
        user.buy(stock, num, stock.nowPrice)

    if sellDays == 0 and isCross == 1 and user.getHoldingStock(stock) is not None:  # 出售日期小于天数  并且  有突破
        sellDays = nowDays + 5  # 设置下次出售日期
        print("预计:", sellDays, "日出售")
    if sellDays >= nowDays and isUpAvg == False:  # 出售日期未到  并且  低于日均
        print("【取消出售】")
        sellDays = 0  # 重置出售日期
    if sellDays == nowDays and nowDays != 0:  # 出售  重置购买日期
        holding = user.getHoldingStock(stock)
        if holding is not None:
            user.sell(stock, holding.num, stock.nowPrice)


def role2(stock):
    global buyDays, sellDays, isCross, nowDays
    avg = tl.MA(stock.getOldDayData.close_price.values[-50:].astype(float), 5)
    nowDays += 1
    isCross = 0
    if len(stock.oldMinData) < 2:
        return
    if float(stock.oldMinData.open.values[-2]) > float(avg[-2]) and \
            float(stock.oldMinData.open.values[-1]) > float(avg[-1]):  # 突破
        print("↑↑↑↑突破↑↑↑↑")
        isCross = 1

    if float(stock.oldMinData.open.values[-2]) > float(avg[-2]) and \
            float(stock.oldMinData.open.values[-1]) < float(avg[-1]):  # 跌破
        print("↓↓↓↓跌破↓↓↓↓")
        isCross = -1

    if float(stock.oldMinData.open.values[-1]) < float(avg[-1]):
        isUpAvg = False
        print("<<<<低于日均<<<<")
    else:
        isUpAvg = True
        print(">>>>高于日均>>>>")

    if buyDays == 0 and isCross == -1 and user.money >= stock.nowPrice:  # 购买日期小于天数  并且  有跌破
        buyDays = nowDays + 5  # 设置下次购买日期
        print("预计:", buyDays, "日购买")
    if buyDays >= nowDays and isUpAvg == True:  # 购买日期未到  并且  高于日均
        print("【取消购买】")
        buyDays = 0  # 重置购买日期
    if buyDays == nowDays and nowDays != 0:  # 收购  重置购买日期
        buyDays = 0
        num = user.money // stock.nowPrice
        print(user.buy(stock, num, stock.nowPrice))

    if sellDays == 0 and isCross == 1 and user.getHoldingStock(stock) is not None:  # 出售日期小于天数  并且  有突破
        sellDays = nowDays + 5  # 设置下次出售日期
        print("预计:", sellDays, "日出售")
    if sellDays >= nowDays and isUpAvg == False:  # 出售日期未到  并且  低于日均
        print("【取消出售】")
        sellDays = 0  # 重置出售日期
    if sellDays == nowDays and nowDays != 0:  # 出售  重置购买日期
        holding = user.getHoldingStock(stock)
        if holding is not None:
            print(user.sell(stock, holding.num, stock.nowPrice))


while ST.nextDay():  # 执行下一日
    print("-----------------------------------------")
    print("日", futures.nowDate, futures.nowPrice)

    nowMin = 0
    while ST.nextMin():
        avg = tl.MA(futures.oldMinData.close.values[-100:].astype(float), 30)
        # 建仓---------
        if float(futures.oldMinData.close.values[-2]) > float(avg[-2]) and \
                float(futures.oldMinData.close.values[-1]) < float(avg[-1]):  # 跌破
            allBuyNum = 0.0
            for var in user.holdingFuturesList:
                if var.case == -1:
                    allBuyNum += var.num

            if allBuyNum < user.allMoney / futures.nowPrice * 0.12:
                positions = user.allMoney / futures.nowPrice * 0.03
                print(user.buy(futures, positions, futures.nowPrice, case=-1))
                stopLossPrice = futures.nowPrice * 1.02
                createPrice = futures.nowPrice

        # ---------
        if len(user.holdingFuturesList) > 0:
            avg5 = tl.MA(futures.oldMinData.close.values[-100:].astype(float), 5)
            if futures.nowPrice <= avg5[-1] * 1.002 and createPrice * 0.997:
                stopLossPrice = futures.nowPrice * 1.001

            if futures.nowPrice >= createPrice * 1.02:
                for holding in user.holdingFuturesList:
                    if holding.futures == futures:
                        print(user.sell(futures, holding.num, futures.nowPrice, case=holding.case))

        if futures.nowPrice >= stopLossPrice:

            for holding in user.holdingFuturesList:
                if holding.futures == futures:
                    print(user.sell(futures, holding.num, futures.nowPrice, case=holding.case))

        pass
    user.put()
    print("-----------------------------------------")
    print("止损价", stopLossPrice, "  建仓价", createPrice)
